declare var wx; 
declare var WeixinJSBridge;
class ResultScene extends eui.Component  {
    private startBtn: eui.Image;
    private graphics;
    private math;
    private illation;
    private observation;
    private space;
    private memory;
    private shadow;
    private title1;
    private title2;
    private avator;
    private avatorS;

    private CENTER_POINT = {
        x: 322,
        y: 540
    }

    private SCORE_CONFIG = {
        math:{
            color:0xff6619,
            0:{
                x: 321.5,
                y: 520
            },
            20: {
                x: 321.5,
                y: 510
            },
            40: {
                x:321.5,
                y: 500
            },
            60: {
                x:321.5,
                y: 485
            },
            80: {
                x:321.5,
                y:447
            },
            100: {
                x: 321.5,
                y: 409
            }
        },
        illation:{
            color:0xff5500,
            0:{
                x: 308,
                y: 538
            },
            20: {
                x: 291,
                y: 536
            },
            40: {
                x:276,
                y: 531
            },
            60: {
                x:259,
                y: 529
            },
            80: {
                x:220,
                y:520
            },
            100: {
                x: 179,
                y: 511
            }
        },
        observation:{
            color:0xff884c,
            0:{
                x: 335,
                y: 539
            },
            20: {
                x: 351,
                y: 534
            },
            40: {
                x:369,
                y: 529
            },
            60: {
                x:386,
                y: 526
            },
            80: {
                x:422,
                y:520
            },
            100: {
                x: 464,
                y: 508
            }
        },
        space:{
            color:0xff7733,
            0:{
                x: 329,
                y: 544
            },
            20: {
                x: 341,
                y:569
            },
            40: {
                x:349,
                y: 579
            },
            60: {
                x:362,
                y: 603
            },
            80: {
                x:387,
                y:637
            },
            100: {
                x: 409,
                y: 666
            }
        },
        memory:{
            color:0xff6619,
            0:{
                x: 313,
                y: 555
            },
            20: {
                x: 301,
                y:570
            },
            40: {
                x:292,
                y: 581
            },
            60: {
                x:282,
                y: 600
            },
            80: {
                x:256,
                y:634
            },
            100: {
                x: 234,
                y: 669
            }
        }
    }
    
    public constructor() {
        super();
        this.skinName = 'result';
        egret.ImageLoader.crossOrigin = "anonymous"
        this.avator.source = AVATOR;
        this.removeChild(this.avatorS);
        setTimeout(() => {
            this.addChild(this.avatorS);
        },0)

        var u = navigator.userAgent;
        var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
        if (isIOS) {        
            if (screen.height == 812 && screen.width == 375){
                this.y = 100;
            }
        }
    }
    public draw(score){
        let SCORE_REGISTER = score || SCORE || {
            math: 0,    //数感
            illation: 0,    //推理
            observation: 0, //观察
            space: 0,   //空间感
            memory: 0   //记忆力
        };

        let sortArr = [{
            type:'math',
            score:SCORE_REGISTER.math,
            important:5
        },{
            type:'illation',
            score:SCORE_REGISTER.illation,
            important:4
        },{
            type:'observation',
            score:SCORE_REGISTER.observation,
            important:3
        },{
            type:'memory',
            score:SCORE_REGISTER.memory,
            important:2
        },{
            type:'space',
            score:SCORE_REGISTER.space,
            important:1
        }]

        sortArr.sort((a, b) => {
            let result;

            if(a.score > b.score){
                result = false;
            }else if(a.score === b.score && a.important > b.important){
                result = false;
            }else {
                result = true;
            }
            return result
        })

        if(sortArr[0].type === 'math'){
            this.title1.text = '孩子的数感超级棒';
            this.title2.text = '数感是数学学习的必备条件'
        }else if(sortArr[0].type === 'illation'){
            this.title1.text = '孩子的推理超级棒';
            this.title2.text = '推理是思维能力高低的表现 '
        }else if(sortArr[0].type === 'observation'){
            this.title1.text = '孩子的观察超级棒';
            this.title2.text = '观察是第一步，没有观察就不会有接踵而来的进步'
        }else if(sortArr[0].type === 'space'){
            this.title1.text = '孩子的空间超级棒';
            this.title2.text = '空间感比知识更重要，因为知识是有限的，而想象要概括世界的一切。 '
        }else if(sortArr[0].type === 'memory'){
            this.title1.text = '孩子的记忆力超级棒';
            this.title2.text = '记忆力是知识留存及学习进步的基础 '
        }
        
        if(CANREAD) {
            this.visible = true;
            this.showResult(score);
        }else{
            this.visible = false;
            for(let key in SCORE_REGISTER) {
                this.removeChild(this[key]);
            }
            SHOWRESULT = () => {
                if(!CANREAD){
                    $('.result-btn').hide();
                    $('.shareBg').hide();
                    this.showResult(score);
                    CANREAD = true;
                }
                $.ajax({
                    url: GLOBAL_PATH + '/question/report/operate',
                    method:'POST',
                    headers: {
                        'Content-Type': "application/json;charset=UTF-8"
                    },
                    data: JSON.stringify({userId: USER_ID, reportId: QSID, operateType: 2}),
                    success:(res) => {
                       
                    }
                })
            }
            $('.result-btn').show()

            $('.shareBtn').on('click',function() {
                $('.shareBg').show();
            })

            let canPay = true;
            let that = this;

            $('.payBtn').on('click',function() {
                if(!canPay) return;
                canPay = false;
                function onBridgeReady(option){
                    canPay = true;
                    WeixinJSBridge.invoke(
                    'getBrandWCPayRequest', {
                        "appId": option.appId,     //公众号名称，由商户传入     
                        "timeStamp": option.timeStamp,         //时间戳，自1970年以来的秒数     
                        "nonceStr": option.nonceStr, //随机串     
                        "package": option.package,     
                        "signType": option.signType,         //微信签名方式：     
                        "paySign": option.paySign //微信签名 
                    },
                    function(res){
                        if(res.err_msg == "get_brand_wcpay_request:ok" ){
                            if(!CANREAD){
                                $('.result-btn').hide();
                                $('.shareBg').hide();
                                that.showResult(score);
                                 CANREAD = true;
                            }
                        } 
                    }); 
                }
                if (typeof WeixinJSBridge == "undefined"){
                    if( document.addEventListener ){
                        document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                    }
                }else{
                    $.ajax({
                        url: GLOBAL_PATH + '/questionpay',
                        method:'GET',
                        data: {userId: USER_ID, orderId: QSID},
                        success:(res) => {
                             onBridgeReady(res);
                        }
                    })
                }
            })
        }
    }
    private showResult(score) {
        this.visible = true;
        $('.result-btn2').show();
        $('.payBtn2').on('click',() => {
            $.ajax({
                url: GLOBAL_PATH + '/question/report/operate',
                method:'POST',
                headers: {
                    'Content-Type': "application/json;charset=UTF-8"
                },
                data: JSON.stringify({userId: USER_ID, reportId: QSID, operateType: 3}),
                success:(res) => {
                }
            })
            $('.last').show();
        })
        let SCORE_REGISTER = score || SCORE || {
            math: 0,    //数感
            illation: 0,    //推理
            observation: 0, //观察
            space: 0,   //空间感
            memory: 0   //记忆力
        };

        // this.removeChild(this.shadow)

        for(let key in SCORE_REGISTER) {
            this.addChild(this[key]);
            this[key].text = this[key].text + SCORE_REGISTER[key] + '分';
        }

        this.drawTriangle(this.SCORE_CONFIG.math[SCORE_REGISTER.math],this.SCORE_CONFIG.observation[SCORE_REGISTER.observation],this.SCORE_CONFIG.math.color)
        this.drawTriangle(this.SCORE_CONFIG.observation[SCORE_REGISTER.observation],this.SCORE_CONFIG.space[SCORE_REGISTER.space],this.SCORE_CONFIG.observation.color)
        this.drawTriangle(this.SCORE_CONFIG.space[SCORE_REGISTER.space],this.SCORE_CONFIG.memory[SCORE_REGISTER.memory],this.SCORE_CONFIG.space.color)
        this.drawTriangle(this.SCORE_CONFIG.memory[SCORE_REGISTER.memory],this.SCORE_CONFIG.illation[SCORE_REGISTER.illation],this.SCORE_CONFIG.memory.color)
        this.drawTriangle(this.SCORE_CONFIG.illation[SCORE_REGISTER.illation],this.SCORE_CONFIG.math[SCORE_REGISTER.math],this.SCORE_CONFIG.illation.color)

        var gameDiv = document.getElementById('gameDiv');
        var shareImage = document.createElement('img') as HTMLImageElement;
        shareImage.id = 'imageI';
        shareImage.src = '';
        shareImage.style.position = 'absolute';
        shareImage.style.width = '100%';
        shareImage.style.height = '100%';
        shareImage.style.opacity = '0';
        gameDiv.appendChild(shareImage);

        setTimeout(() => {
            let renderTexture = new egret.RenderTexture();
            renderTexture.drawToTexture(GLOBAL_IMAGE)
            shareImage.src = renderTexture.toDataURL('image/png');
        }, 1000)
    }
    private drawTriangle(point1, point2, color) {
        var shape:egret.Shape = new egret.Shape();
        shape.graphics.lineStyle(1, color, 0.7);
        shape.graphics.beginFill(color,0.9);
        shape.graphics.moveTo(this.CENTER_POINT.x,this.CENTER_POINT.y);
        shape.graphics.lineTo(point1.x, point1.y);
        shape.graphics.lineTo(point2.x, point2.y);
        shape.graphics.lineTo(this.CENTER_POINT.x,this.CENTER_POINT.y);
        shape.graphics.endFill();
        this.addChild(shape);
    }
}